import axios from "axios";
import Qs from "qs";
import messageAlert from "../assets/js/newMessage";

// axios中请求配置有baseURL选项，表示请求URL公共部分。
// `baseURL` 将自动加在 `url` 前面，除非 `url` 是一个绝对 URL
// 配置axios的默认URL
if (process.env.NODE_ENV === "production") {
  axios.defaults.baseURL = "https://minitool.xuexiwangzhe.com/miniapp";
} else {
  axios.defaults.baseURL = "https://minitool.xuexiwangzhe.com/miniapp";
}
// 配置允许跨域
axios.defaults.crossDomain = true;
// 配置允许跨域携带cookie
axios.defaults.withCredentials = true;
// 配置超时时间
axios.defaults.timeout = 1e3 * 30;
// 配置请求Header

axios.defaults.headers["*"] = "Access-Control-Allow-Origin";
// axios.defaults.headers['Content-Type']='Access-Control-Allow-Headers;application/json;charset=utf-8;Access-Control-Allow-Origin;';
axios.defaults.headers["Content-Type"] = "application/json;charset=utf-8;";
axios.defaults.headers["Authorization"] = "Access-Control-Allow-Headers";
axios.defaults.headers["Accept"] = "application/json;charset=utf-8;";

// axios request拦截 添加用户身份标识
axios.interceptors.request.use(
  (config) => {
    // var SESSIONKEY =
    //   "5a8a40116b6102643a9f8a51963d004da1a1abffebef9895656b4e7d8dc155f64b91dbfb1d4247a2765d45c3ab9da162";
    // window.sessionStorage.setItem("SESSIONKEY", SESSIONKEY);
    let SESSIONKEY = "";
    if (process.env.NODE_ENV === "production") {
      SESSIONKEY = window.sessionStorage.getItem("SESSIONKEY");
    } else {
      // SESSIONKEY =
      //   "30f059803f4a8d3adf3f5c9181ad1e2380be541b8696f7257112100f57d54c09"; // 测试
      // window.sessionStorage.setItem("SESSIONKEY", SESSIONKEY);
      SESSIONKEY = window.sessionStorage.getItem("SESSIONKEY");
    }
    if (SESSIONKEY) {
      config.headers.SESSIONKEY = SESSIONKEY;
    }
    return config;
  },
  (error) => {
    return Promise.reject(error);
  }
);

// axios response拦截，统一处理响应错误
axios.interceptors.response.use(
  (response) => {
    if (!response.data.success) {
      messageAlert.error(response.data.msg);
    }
    return Promise.resolve(response.data);
  },
  (error) => {
    console.log("[interceptors] - [response] - error", error);
    messageAlert.error(error);
    if (error.response) {
      let _status = String(error.response.status);
      if (["200", "304", "400"].includes(_status)) {
        return Promise.response(error.response);
      }
    }
    return Promise.reject(error);
  }
);
// get请求
export function get(url, params) {
  return new Promise((resolve, reject) => {
    axios
      .get(url, {
        params: Qs.stringify(params),
      })
      .then((res) => {
        if (res.success) {
          resolve(res.msg);
        } else {
          reject(res.msg);
        }
      })
      .catch((err) => {
        console.error("[get] - err", err);
      });
  });
}
// post请求
export function post(url, params) {
  return new Promise(function(resolve, reject) {
    console.log(function() {
      return Qs.stringify(params);
    });
    axios
      .post(url, params)
      .then((res) => {
        if (res.success) {
          resolve(res.msg);
        } else {
          reject(res.msg);
        }
      })
      .catch((err) => {
        console.error("[post] - err", err);
      });
  });
}
